home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / overview / dtscpluslibrary / sources / collectionclassestest.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  6.4 KB  |  263 lines

  1. /*
  2.     File:        CollectionClassesTest.cp
  3.  
  4.     Contains:    The following collection classes are implemented: TLinkedList, TStack, (TQueue, TDeQueue),
  5.                   THashTable.
  6.                   CollectionClasses.cp contains the collection class member functions. 
  7.  
  8.     Written by: Kent Sandvik    
  9.  
  10.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  11.  
  12.                 You may incorporate this Apple sample source code into your program(s) without
  13.                 restriction. This Apple sample source code has been provided "AS IS" and the
  14.                 responsibility for its operation is yours. You are not permitted to redistribute
  15.                 this Apple sample source code as "Apple sample source code" after having made
  16.                 changes. If you're going to re-distribute the source, we require that you make
  17.                 it clear in the source that the code was descended from Apple sample source
  18.                 code, but that you've made changes.
  19.  
  20.     Change History (most recent first):
  21.                 8/18/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  22.                 
  23.  
  24. */
  25. #ifndef _COLLECTION
  26. #include "CollectionClasses.h"
  27. #endif
  28.  
  29. // FLAGS that will enable/disable tests
  30. //#define TLINKEDLIST
  31. //#define TSTACK
  32. //#define TQUEUE
  33. #define TDEQUE
  34. //#define THASHTABLE
  35.  
  36. void DoSomething(THashEntryPtr p);                // our MapCar test function
  37.  
  38.  
  39. void main(void)
  40. {
  41.     cout << "Start of collection class tests…\n";
  42.  
  43. // _________________________________________________________________________________________________________ //
  44. // TLINKEDLIST TEST
  45.  
  46. #ifdef TLINKEDLIST
  47.     cout << "\nTest1: TLinkedList test…\n";
  48.  
  49.     //     Create a TLinkedList
  50.  
  51.     TLinkedList myList;
  52.  
  53.     //    Append elements to the list
  54.  
  55.     myList.Append(1);
  56.     myList.Append(2);
  57.     myList.Append(3);
  58.     myList.Append(4);
  59.     // Show values in list
  60.     myList.Reset();
  61.     long val;
  62.     while ((val = myList.Next()) != NULL)
  63.         cout << "TLinkedList Entry =  " << val << "\n";
  64.  
  65.     // Show first entry
  66.     cout << "First entry = " << myList.First() << "\n";
  67.  
  68.     // Show last entry
  69.     cout << "Last entry = " << myList.Last() << "\n";
  70.  
  71.     // Try to find entry
  72.     cout << "Trying to find entry 3…\n";
  73.     Boolean OK = myList.Find(3);
  74.     if (OK)
  75.         cout << "We found 3, OK!\n";
  76.     else
  77.         cout << "Ouch, problems, time to debug…";
  78.  
  79.  
  80.     // Remove two entries, 2 and 4
  81.     myList.Remove(2);
  82.     myList.Remove(4);
  83.     cout << "Removed 2 and 4, list looks now like:\n";
  84.     myList.Reset();
  85.     while ((val = myList.Next()) != NULL)
  86.         cout << "TLinkedList Entry =  " << val << "\n";
  87.  
  88. #endif
  89.  
  90. // _________________________________________________________________________________________________________ //
  91. // TSTACK TEST
  92.  
  93. #ifdef TSTACK    
  94.     //    TSTACK TEST PHASE
  95.     cout << "Test2: test TStack:\n";
  96.  
  97.     TStack myStack;
  98.  
  99.     myStack.Push(1 L);
  100.     myStack.Push(2 L);
  101.     myStack.Push(3 L);
  102.     myStack.Push(4 L);
  103.     myStack.Push(5 L);
  104.  
  105.     // test our linked list properties
  106.     cout << "First entry =  " << myStack.First() << "\n";
  107.     cout << "Last entry =  " << myStack.Last() << "\n";
  108.  
  109.     // test our next loop
  110.     myStack.Reset();
  111.     long val;
  112.     while ((val = myStack.Next()) != NULL)
  113.         cout << "Entry =  " << val << "\n";
  114.  
  115.  
  116.     // test Find
  117.     long item1 = 4 L;
  118.     long item2 = 44 L;
  119.     Boolean outcome;
  120.  
  121.     outcome = myStack.Find(item1);
  122.     if (outcome)
  123.         cout << "We have item1 in queue\n";            // should trigger
  124.     else
  125.         cout << "We don't have item1 in queue\n";
  126.  
  127.     outcome = myStack.Find(item2);
  128.     if (outcome)
  129.         cout << "We have item2 in queue\n";
  130.     else
  131.         cout << "We don't have item2 in queue\n";    // should trigger
  132.  
  133.  
  134.     // test out stack properties    
  135.     while (!myStack.IsEmpty())
  136.         cout << "Popping from the stack, value = " << myStack.Pop() << "\n";
  137.  
  138.     cout << "End of TStack test!\n";
  139.  
  140. #endif
  141.  
  142. // _________________________________________________________________________________________________________ //
  143. // TQUEUE TEST
  144.  
  145. #ifdef TQUEUE
  146.     cout << "\nTest3: TQueue test…\n";
  147.  
  148.     // Create TQueue
  149.     TQueue myQueue;
  150.  
  151.     // Place stuff in the queue
  152.     myQueue.Put(10);
  153.     myQueue.Put(20);
  154.     myQueue.Put(30);
  155.     myQueue.Put(40);
  156.  
  157.  
  158.     // Test out .Last()
  159.     cout << "Last, or actually the first pushed entry is = " << myQueue.Last() << "\n";
  160.  
  161.     // get out 2 more values
  162.     cout << " Get =  " << myQueue.Get();
  163.     cout << " Get = " << myQueue.Get();
  164.     cout << "\nI will get out two more values, and the next last value is 30 = " << myQueue.Last() << "\n";
  165.  
  166.  
  167.     // Get Stuff from queue
  168.  
  169.     while (!myQueue.IsEmpty())
  170.         cout << "Get values from the queue, value = " << myQueue.Get() << "\n";
  171.  
  172. #endif
  173.  
  174. // _________________________________________________________________________________________________________ //
  175. // TDEQUE TEST
  176.  
  177. #ifdef TDEQUE
  178.     cout << "\nTest4: TDeque test…\n";
  179.  
  180.     // Create TDeque
  181.     TDeque myDeque;
  182.  
  183.     // Place stuff at end of deque
  184.     myDeque.Push(11);
  185.     myDeque.Push(12);
  186.     myDeque.Push(13);
  187.     myDeque.Push(14);
  188.  
  189.     myDeque.PutAtEnd(21);
  190.     myDeque.PutAtEnd(22);
  191.  
  192.     cout << "Pop (14) = " << myDeque.Pop() << "\n";
  193.     cout << "Pop (13) = " << myDeque.Pop() << "\n";
  194.     cout << "Get (22) = " << myDeque.Get() << "\n";
  195.     
  196.     cout << "The list should now look like: 21 11 12\n";
  197.  
  198.     // empty the deque
  199.     while (!myDeque.IsEmpty())
  200.         cout << "Get values from the dequeue, value = " << myDeque.Get() << "\n";
  201.  
  202. #endif
  203.  
  204.  
  205. // _________________________________________________________________________________________________________ //
  206. // THASHTABLE TEST
  207.  
  208. #ifdef THASHTABLE
  209.     //    THASHTABLE TEST PHASE
  210.     cout << "\nTest6: THashTable test…\n";
  211.  
  212.     // Create Hashtable.    
  213.     THashTable myHashTable;
  214.  
  215.     // Add entries to the Hashtable.    
  216.     myHashTable.Add(4, 100);
  217.     myHashTable.Add(5, 400);
  218.     myHashTable.Add(6, 500);
  219.  
  220.     // Find entry/value in HashTable
  221.  
  222.     long tableValue = myHashTable.Find(4);
  223.     cout << "The value is " << tableValue << ", and should be 100\n";
  224.  
  225.     // remove this entry
  226.     myHashTable.Remove(4);
  227.  
  228.     // now try to find it, and signal we had problems
  229.     TItemtype entry = myHashTable.Find(4);
  230.     if (entry == 0)
  231.         cout << "THashTable.Remove() seems to work fine!\n";
  232.     else
  233.         cout << "Eh, a value, and we just deleted the entry, start debugging THashTable.Remove()!\n";
  234.  
  235.     // MapCar test
  236.     myHashTable.MapCar((MapFun)DoSomething);
  237.  
  238.     cout << "End of THashTable test…\n";
  239. #endif
  240.  
  241.     cout << "End of collection class tests!\n";
  242. }
  243.  
  244.  
  245. // This function is needed for the THashTable.MapCar test
  246. void DoSomething(THashEntryPtr p)
  247. {
  248.     // print out the value inside each THashEntryPtr…
  249.     cout << "Hash Table entry = " << p->fValue << "\n";
  250. }
  251.  
  252.  
  253. // _________________________________________________________________________________________________________ //
  254.  
  255.  
  256. /*    Change History (most recent last):
  257.   No        Init.    Date        Comment
  258.   1            khs        11/7/92        New file
  259.   2            khs        11/28/92    Added TLinkedList
  260.   3            khs        11/29/92    Added TQueue and TDeque
  261.   4            khs        1/14/93        Cleanup
  262. */
  263.